package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.TableNameConstant;
import cc.rengu.igas.bomp.common.dao.BizTmpMapper;
import cc.rengu.igas.bomp.common.entity.BizTmp;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/10
 */
public class BizTmpMapperImpl implements BizTmpMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertBizTmp(BizTmp bizTmp) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBizTmp");

        /**默认值赋值*/
        defaultBizTmpInfo(bizTmp);
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, bizTmp, null);
        if (0 != iReturnCode) {
            rglog.error("登记业务临时表信息失败,instId:<{}>,realId:<{}>,tableName:<{}>,iReturnCode:<{}>",
                    bizTmp.getInstId(), bizTmp.getRealId(), bizTmp.getOprTableEname(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBizTmpByRealId(BizTmp bizTmp) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBizTmpById");

        /**默认值赋值*/
        defaultBizTmpInfo(bizTmp);
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, bizTmp, null);
        if (0 != iReturnCode) {
            rglog.error("更新业务临时表信息失败,instId:<{}>,realId:<{}>,tableName:<{}>,iReturnCode:<{}>",
                    bizTmp.getInstId(), bizTmp.getRealId(), bizTmp.getOprTableEname(), iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 临时表数据默认值
     */
    private void defaultBizTmpInfo(BizTmp bizTmp) {
        if (StringUtil.isEmptyOrNull(bizTmp.getOprMenuId())) {
            bizTmp.setOprMenuId(BompAppParamConstant.OPR_MENU_ID);
        }
        if (StringUtil.isEmptyOrNull(bizTmp.getOprMenuName())) {
            bizTmp.setOprMenuName(BompAppParamConstant.OPR_MENU_NAME);
        }
        if (StringUtil.isEmptyOrNull(bizTmp.getWebNextUrl())) {
            bizTmp.setWebNextUrl(BompAppParamConstant.WEB_NEXT_URL);
        }
        if (StringUtil.isEmptyOrNull(bizTmp.getDetailFlag())) {
            if (TableNameConstant.T_B_MCHNT_BASE_INFO.equals(bizTmp.getOprTableEname())) {
                bizTmp.setDetailFlag(AppParamConstant.NO);
            } else {
                bizTmp.setDetailFlag(AppParamConstant.YES);
            }
        }
        if (StringUtil.isEmptyOrNull(bizTmp.getMasterShowFlag())) {
            if (TableNameConstant.T_B_MCHNT_BASE_INFO.equals(bizTmp.getOprTableEname())) {
                bizTmp.setMasterShowFlag(AppParamConstant.YES);
            } else {
                bizTmp.setMasterShowFlag(AppParamConstant.NO);
            }
        }
    }

    @Override
    public BizTmp selectBizTmpByRealId(String instId, String realId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBizTmpById");
        BizTmp bizTmp = new BizTmp();
        bizTmp.setInstId(instId);
        bizTmp.setRealId(realId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, bizTmp, bizTmp);
        if (0 != iReturnCode) {
            rglog.error("获取业务临时表信息失败,instId:<{}>,realId:<{}>,iReturnCode:<{}>", instId, realId, iReturnCode);
            return null;
        }
        return bizTmp;
    }

    @Override
    public List<BizTmp> selectBizTmpByManageInstId(String instId, String manageInstId, String oprTableName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBizTmpByManageInstId");
        BizTmp bizTmp = new BizTmp();
        bizTmp.setInstId(instId);
        bizTmp.setManageInstId(manageInstId);
        bizTmp.setOprTableEname(oprTableName);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bizTmp, BizTmp.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取业务临时表信息失败,instId:<{}>,manageInstId:<{}>,oprTableName:<{}>", instId, manageInstId, oprTableName);
            return null;
        }
        return objectList.stream().map(item -> (BizTmp) item).collect(Collectors.toList());
    }

    @Override
    public List<BizTmp> selectBizTmpByMchntNo(String instId, String mchntNo, String oprTableName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBizTmpByMchntNo");
        BizTmp bizTmp = new BizTmp();
        bizTmp.setInstId(instId);
        bizTmp.setMchntNo(mchntNo);
        bizTmp.setOprTableEname(oprTableName);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bizTmp, BizTmp.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取业务临时表信息失败,instId:<{}>,mchntNo:<{}>,oprTableName:<{}>", instId, mchntNo, oprTableName);
            return null;
        }
        return objectList.stream().map(item -> (BizTmp) item).collect(Collectors.toList());
    }


    @Override
    public List<BizTmp> selectBizTmpListByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBizTmpListByMchntNo");
        BizTmp bizTmp = new BizTmp();
        bizTmp.setInstId(instId);
        bizTmp.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bizTmp, BizTmp.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取业务临时表信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (BizTmp) item).collect(Collectors.toList());
    }
}
